/*
Copyright (C) 2008 raohmaru <http://www.raohmaru.com>

This file is part of Raohmaru AS3 Framework.

Raohmaru AS3 Framework is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Raohmaru AS3 Framework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with Raohmaru AS3 Framework.  If not, see <http://www.gnu.org/licenses/>.
*/

package jp.raohmaru.media.filters
{
import flash.media.Sound;
import flash.utils.ByteArray;

/**
 * Genera dinámicamente ruido sinuosidal transmitiéndolo en el objeto de flujo de salida.
 * @author Raohmaru
 * @version 1.0
 */
public class SineWaveNoiseFilter extends NoiseFilter implements IFilter
{
	private const	SIN :Function = Math.sin,
					PI2 :Number = Math.PI/2;
	
	/**
	 * Nuevo objeto NoiseFilter que puede añadirse a lista de filtros de un objeto SoundFX.
	 * @param amplitude Amplitud de onda del ruido generado.
	 * @param blockSize Cantidad de muestras de sonido a procesar.
	 */
	public function SineWaveNoiseFilter(amplitude :Number=.25, blockSize :int=2048)
	{
		super(amplitude, blockSize);
	}
	
	/**
	 * @inheritDoc
	 */
	override public function apply( source :Sound, sample :ByteArray ) :int
	{
		var c :int = _blockSize,
			n :Number;
		while(--c > -1)
		{
			n = SIN( (Number(c+_position)/PI2) ) * _amplitude;
	        sample.writeFloat(n);
	        sample.writeFloat(n);
	    }
	    
	    _position += _blockSize;

		return 1;
	}
}
}